#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <string.h>


int main()
{
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if (clientfd < 0) {
        perror("socket failed");
        return -1;
    }

    struct sockaddr_in addr;
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(8080);

    int n = connect(clientfd, (struct sockaddr*)&addr, sizeof(addr));
    if (n < 0) {
        perror("connection failed");
        return -2;
    }

    char buffer[128] = {0};

    while (1) {
        fgets(buffer, 128, stdin); // 第三个参数 FILE*，从 stdin 中接收
        send(clientfd, buffer, strlen(buffer), 0);

        int n = recv(clientfd, buffer, 128, 0);
        buffer[n] = 0;
        printf("server callback: %s\n", buffer);
    }

    close(clientfd);

    return 0;
}